package com.redlight.base;

import com.alibaba.fastjson.annotation.JSONField;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chao on 2019/1/9.
 */
public class Page implements Serializable {
    public static String pageBeanName = "page";
    public static final int DEFULT_PAGE_SIZE = 20;
    private Integer count = Integer.valueOf(0);
    private int pageNo;
    private int pageCount;
    private int pageSize = 20;
    private int startRowNum;
    private int endRowNum;
    private String orderBy;
    private List rows = new ArrayList();

    public Page() {
    }

    public boolean getIsFirstPage() {
        return this.pageNo <= 1;
    }

    public boolean getIsLastPage() {
        return this.pageCount <= this.pageNo;
    }

    public Integer getCount() {
        return this.count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public int getPageNo() {
        if(this.pageNo < 1 && this.count.intValue() > 0) {
            this.pageNo = 1;
        }

        if(this.pageNo > this.getPageCount()) {
            this.pageNo = this.pageCount;
        }

        return this.pageNo;
    }

    public void setPageNo(int aCurrentPage) {
        if(aCurrentPage >= 0) {
            this.pageNo = aCurrentPage;
        }

    }

    public int getPageCount() {
        if(this.count.intValue() % this.pageSize == 0) {
            this.pageCount = this.count.intValue() / this.pageSize;
        } else {
            this.pageCount = this.count.intValue() / this.pageSize + 1;
        }

        return this.pageCount;
    }

    public void setPageCount(int aPageCount) {
        this.pageCount = aPageCount;
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int aPageSize) {
        this.pageSize = aPageSize;
    }

    public int getEndRowNum() {
        this.endRowNum = this.getStartRowNum() + this.pageSize;
        if(this.endRowNum > this.count.intValue()) {
            this.endRowNum = this.count.intValue();
        }

        if(this.endRowNum > 0) {
            --this.endRowNum;
        }

        return this.endRowNum;
    }

    public void setEndRowNum(int aEndRowNum) {
        this.endRowNum = aEndRowNum;
    }

    public int getStartRowNum() {
        int _pageNo = this.getPageNo();
        int _pageCount = this.getPageCount();
        if(_pageNo > _pageCount) {
            _pageNo = _pageCount;
        }

        this.startRowNum = (_pageNo - 1) * this.pageSize > 0?(_pageNo - 1) * this.pageSize:0;
        return this.startRowNum;
    }

    public void setStartRowNum(int aStartRowNum) {
        this.startRowNum = aStartRowNum;
    }

    public String getOrderBy() {
        if("null".equalsIgnoreCase(this.orderBy)) {
            this.orderBy = "";
        }

        return this.orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public List getRows() {
        return this.rows;
    }

    public void setRows(List rows) {
        this.rows = rows;
    }

    @JSONField(
            serialize = false
    )
    public int getMaxResults() {
        return this.getPageSize();
    }

    public static String getPageBeanName() {
        return pageBeanName;
    }

    public static void setPageBeanName(String pageBeanName) {
        pageBeanName = pageBeanName;
    }
}
